<template>
  <el-form ref="form" :model="state.ruleForm" :rules="state.rules" size="large" class="login-content-form">
    <el-form-item class="login-animation1" prop="username">
      <el-input
        type="text"
        :placeholder="$t('message.account.accountPlaceholder1')"
        v-model="state.ruleForm.username"
        clearable
        autocomplete="off"
      >
        <template #prefix>
          <el-icon class="el-input__icon"><ele-User /></el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item class="login-animation2" prop="password">
      <el-input
        :type="state.isShowPassword ? 'text' : 'password'"
        :placeholder="$t('message.account.accountPlaceholder2')"
        v-model="state.ruleForm.password"
        autocomplete="off"
      >
        <template #prefix>
          <el-icon class="el-input__icon"><ele-Unlock /></el-icon>
        </template>
        <template #suffix>
          <i
            class="iconfont el-input__icon login-content-password"
            :class="state.isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
            @click="state.isShowPassword = !state.isShowPassword"
          >
          </i>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item class="login-animation3" prop="captcha">
      <el-col :span="15" :xs="12">
        <el-input
          type="text"
          maxlength="4"
          placeholder="验证码"
          v-model="state.ruleForm.answer"
          clearable
          autocomplete="off"
        >
          <template #prefix>
            <el-icon class="el-input__icon"><ele-Position /></el-icon>
          </template>
        </el-input>
      </el-col>
      <el-col :span="1"></el-col>
      <el-col :span="8" :xs="11">
        <el-button class="login-content-code" v-waves @click="refreshCaptcha">
          <img :src="captchaUrl" style="width: 100%; height: 100%; margin-top: -5px;" />
        </el-button>
      </el-col>
    </el-form-item>
    <el-form-item class="login-animation4">
      <el-button
        type="primary"
        class="login-content-submit"
        round
        v-waves
        @click="onSignIn"
        :loading="state.loading.signIn"
      >
        <span>{{ $t('message.account.accountBtnText') }}</span>
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts" name="loginAccount">
import { reactive, ref, onMounted } from 'vue';
import { FormInstance } from 'element-plus';
import { login } from '/@/api/login';
import { createPassword } from '/@/utils/createPassword';
import { useLoginSuccess } from '../hook/use-login-success';

const loginSuccess = useLoginSuccess();
const form = ref<FormInstance>();
const state = reactive({
  isShowPassword: false,
  ruleForm: {
    username: process.env.NODE_ENV === 'development' ? 'admin' : '',
    password: process.env.NODE_ENV === 'development' ? '123456' : '',
    answer: process.env.NODE_ENV === 'development' ? '1234' : ''
  },
  rules: {
    username: [{ required: true, message: '请输入用户名' }],
    password: [{ required: true, message: '请输入密码' }],
    answer: [{ required: true, message: '请输入验证码' }],
  },
  loading: {
    signIn: false,
  },
  captcha: '',
  captcha_id: '',
});

const captchaUrl = ref('/api/kaptcha')


onMounted(() => {
  refreshCaptcha();
});

// 刷新验证码
const refreshCaptcha = () => {
  captchaUrl.value = `/api/kaptcha?time=${new Date().getTime()}`;
};

// 登录
const onSignIn = async () => {
  await form.value?.validate();
  state.loading.signIn = true;

  try {
    const token = await login({
      ...state.ruleForm,
    });

    await loginSuccess(token);
  } catch (err) {
    refreshCaptcha();
    state.loading.signIn = false;
  }
};
</script>

<style scoped lang="scss">
.login-content-form {
  margin-top: 20px;
  @for $i from 1 through 4 {
    .login-animation#{$i} {
      opacity: 0;
      animation-name: error-num;
      animation-duration: 0.5s;
      animation-fill-mode: forwards;
      animation-delay: calc($i/10) + s;
    }
  }
  .login-content-password {
    display: inline-block;
    width: 20px;
    cursor: pointer;
    &:hover {
      color: #909399;
    }
  }
  .login-content-code {
    width: 100%;
    padding: 0;
    font-weight: bold;
    letter-spacing: 5px;
  }
  .login-content-submit {
    width: 100%;
    letter-spacing: 2px;
    font-weight: 300;
    margin-top: 15px;
  }
}
</style>
